/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nhncorp.neptune.changelog;

import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

import junit.framework.TestCase;

import com.nhncorp.neptune.client.Cell;
import com.nhncorp.neptune.client.Row;
import com.nhncorp.neptune.changelog.pipe.Bulk;
import com.nhncorp.neptune.common.Constants;
import com.nhncorp.neptune.common.conf.NConfiguration;
import com.nhncorp.neptune.neptune_test.common.MiniChangeLogCluster;
import com.nhncorp.neptune.tabletserver.ChangeLog;

public class TestChangeLogClient extends TestCase {
  MiniChangeLogCluster cluster;
  NConfiguration conf = new NConfiguration();
  static byte[] largeBytes;
  
  static {
    largeBytes = new byte[Bulk.DEFAULT_BUFFER_SIZE];
    
    for(int i = 0; i < largeBytes.length; i++) {
      largeBytes[i] = (byte) (new Random().nextInt() % 0xff);
    }
  }

  protected void setUp() throws Exception {
    cluster = MiniChangeLogCluster.create(conf);
  }

  protected void tearDown() throws Exception {
    cluster.shutdown();
  }
  
  /*
  public void testRollbackSimpleMessage() {
    ChangeLogClient client = null;
    
    try {
      String content1 = "HelloWorld";
      String content2 = "MoonSangchul";
      
      client = new ChangeLogClient(conf, "TEST_TABLET_" + System.currentTimeMillis());
      client.open();
      
      client.startWriting("id1");
      client.write(content1.getBytes());
      int fileLen = client.commitWriting();
      
      assertEquals(fileLen, client.rollbackPosition);
      
      client.startWriting("id2");
      client.write(content2.getBytes());
      client.commitWriting();
      
      client.rollbackPosition = fileLen;
      
      client.rollback();
      
      assertEquals(fileLen, client.getLogFileSize());
    } catch (IOException e) {
      e.printStackTrace();
      assertTrue(false);
    } finally {
      try {
        client.close();
      } catch(Exception e) {
        e.printStackTrace();
      }
    }
  }
  
  public void testRollbackChangeLog() {
    try {
      ChangeLog[] insertLogList = createChangeLogList(Constants.LOG_OP_ADD_COLUMN_VALUE);
      
      ChangeLogClient client = new ChangeLogClient(conf, "TEST_TABLET_" + System.currentTimeMillis());
      client.open();
      
      client.startWriting("id1");
      
      for(ChangeLog log : insertLogList) {
        client.append(log);
      }
      
      long len = client.commitWriting();
      
      ChangeLog[] deleteLogList = createChangeLogList(Constants.LOG_OP_DELETE_COLUMN_VALUE);
      
      client.startWriting("id2");
      
      for(ChangeLog log : deleteLogList) {
        client.append(log);
      }
      
      client.commitWriting();
      
      TransactionData[] dataList = client.readLastLogFile();

      assertEquals(2, dataList.length);
      
      assertEquals(new String("id1"), new String(dataList[0].id));
      assertEquals(new String("id2"), new String(dataList[1].id));
      
      assertTrue(Arrays.equals(dataList[0].logList, insertLogList));
      assertTrue(Arrays.equals(dataList[1].logList, deleteLogList));

      client.rollbackPosition = len;
      client.rollback();
      
      TransactionData[] rollbackedList = client.readLastLogFile();
      
      assertEquals(1, rollbackedList.length);
      
      assertEquals(new String("id1"), new String(rollbackedList[0].id));
      
      assertTrue(Arrays.equals(rollbackedList[0].logList, insertLogList));
    } catch(IOException e) {
      e.printStackTrace();
      assertTrue(false);
    }
  }
  */
  
  public void testPutLargeData() {
    ChangeLog[] insertLogList = null;
    try {
      insertLogList = createLargeChangeLogList(Constants.LOG_OP_ADD_COLUMN_VALUE);
    } catch (IOException e1) {
      e1.printStackTrace();
      assertTrue(false);
    }
    
    ChangeLogClient client = null;
    try {
      client = new ChangeLogClient(conf, "TEST_TABLET_" + System.currentTimeMillis());
      client.removeAllLogs();
      
      client.open();
      
      client.startWriting("id1");
      
      for(ChangeLog log : insertLogList) {
        client.append(log);
      }
      
      int size = client.commitWriting();
      System.out.println("size : " + size);
      TransactionData[] log = client.readLastLogFile();
      
      assertEquals(1, log.length);
      assertEquals(3, log[0].logList.length);
      assertEquals(size, client.getLogFileSize());
      
      byte[] logData = log[0].logList[0].getValue();
      assertEquals(largeBytes.length, logData.length);
      
      for(int i = 0; i < largeBytes.length; i++) {
        if (largeBytes[i] != logData[i]) {
          System.out.println("index : " + i + " is different");
          assertTrue(false);
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
      assertTrue(false);
    }
  }

  private ChangeLog[] createChangeLogList(int operation) throws IOException {
    ChangeLog[] logList = new ChangeLog[3];
    for(int i = 0; i < logList.length; i++) {
      long timestamp = System.currentTimeMillis();
      logList[i] = new ChangeLog(operation, 
          "TEST_TABLET", 
          new Row.Key("TEST_ROWKEY_" + timestamp),
          "TEST_COLUMN_NAME",
          new Cell.Key("TEST_COLUMN_KEY_" + timestamp),
          timestamp,
          String.valueOf(timestamp).getBytes());  
    }
    return logList;
  }
  
  private ChangeLog[] createLargeChangeLogList(int operation) throws IOException {
    ChangeLog[] logList = new ChangeLog[3];
    for(int i = 0; i < logList.length; i++) {
      long timestamp = System.currentTimeMillis();
      logList[i] = new ChangeLog(operation, 
          "TEST_TABLET", 
          new Row.Key("TEST_ROWKEY_" + timestamp),
          "TEST_COLUMN_NAME",
          new Cell.Key("TEST_COLUMN_KEY_" + timestamp),
          timestamp,
          largeBytes);  
    }
    return logList;
  }
}
